﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace FunctionalProgramming.PatternMatching.CollectionMatchingRules
{
   internal class NumberOfElementsAndRest<T> : ICollectionMatchingRule<T>
   {
      private readonly Action<T[], IEnumerable<T>> action;
      private readonly int numberOfElements;

      public NumberOfElementsAndRest(Action<T[], IEnumerable<T>> action, int numberOfElements)
      {
         this.action = action;
         this.numberOfElements = numberOfElements;
      }

      public bool DoesCollectionMatch(IEnumerable<T> collection)
      {
         bool result = collection.Count() >= numberOfElements;
         return result;
      }

      public void ExecuteWithCollection(IEnumerable<T> collection)
      {
         T[] headElements = collection.Take(numberOfElements).ToArray();
         IEnumerable<T> rest = collection.Skip(numberOfElements);
         action(headElements, rest);
      }
   }
}